package dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.R;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

import GsonBean.JcxxChakanBean;
import GsonBean.SuccessBean;
import GsonBean.post.TjjdjcdPost;
import GsonBean.post.TxzgqkPost;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import Utils.MyToast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by zx on 2017/4/13.
 */
//填写整改情况
public class TxzgqkDialog extends BaseDialog {

    EditText edittext;
    TextView queding;
    TextView quxiao;
    TxzgqkPost mTxzgqkPost;
    JcxxChakanBean mJcxxChakanBean;

    Activity baseActivity;

    public TxzgqkDialog(Context context, JcxxChakanBean mJcxxChakanBean, Activity baseActivity) {
        super(context);
        this.mJcxxChakanBean = mJcxxChakanBean;
        this.baseActivity = baseActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_txzgqk);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        edittext = (EditText) findViewById(R.id.edittext);
        queding = (TextView) findViewById(R.id.queding);
        quxiao = (TextView) findViewById(R.id.quxiao);
    }

    @Override
    protected void InitData() {
        mTxzgqkPost = new TxzgqkPost();

    }

    @Override
    protected void InitListen() {
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TxzgqkDialog.this.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(edittext.getText().length()>0&&edittext.getText().toString().trim().length()==0){
                    Toast.makeText(getContext(),"请不要输入空格",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(edittext.getText().length()==0){
                    Toast.makeText(getContext(),"请输入整改意见",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!edittext.getText().toString().equals("")) {
                    MyApplication.ShowWaitDialog(getContext());
                    mTxzgqkPost.setJdjhjcbId(mJcxxChakanBean.getRjdjhjcbInfo().getJdjhjcbId());
                    mTxzgqkPost.setZgqk(edittext.getText().toString());
                    mTxzgqkPost.setZrDwMc(mJcxxChakanBean.getRjdjhjcbInfo().getZrDwMc());
                    mTxzgqkPost.setJcbstatus(mJcxxChakanBean.getRjdjhjcbInfo().getJcbstatus());
                    mTxzgqkPost.setJcbfcrId(mJcxxChakanBean.getRjdjhjcbInfo().getJcbfcrId());
                    mTxzgqkPost.setFldw(mJcxxChakanBean.getRjdjhjcbInfo().getFldw());
                    mTxzgqkPost.setXgrId(MyApplication.getmLoginUser().getUserDisplayName());

                    //审核单位流转信息list
                    List<TjjdjcdPost.LjyyxRjdjhjcbShdw> jcbShdwList = new ArrayList<TjjdjcdPost.LjyyxRjdjhjcbShdw>();
                    TjjdjcdPost.LjyyxRjdjhjcbShdw shdw = new TjjdjcdPost.LjyyxRjdjhjcbShdw();
                    shdw.setJdjhbBh(mJcxxChakanBean.getRjdjhjcbInfo().getRjdjhbBh());
                    shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                    shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    jcbShdwList.add(shdw);
                    mTxzgqkPost.setJcbShdwList(jcbShdwList);

                    NetWorkApi.Txzgqk(mTxzgqkPost, new Callback<SuccessBean>() {
                                @Override
                                public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                    SuccessBean mBean = response.body();
                                    try {
                                        if (mBean.getMsg().equals("success") && mBean.getCount() != 0) {
                                            MyToast.showToast("填写整改情况成功");
                                            RefreshManager.Jdjc.RefreshJdjclrxx();
                                            RefreshManager.Jdjc.RefreshJdjcdcl();
                                            RefreshManager.Jdjc.RefreshJdjcycl();
                                            TxzgqkDialog.this.dismiss();
                                            getContext().startActivity(new Intent(getContext(), JclrActivity.class));
                                        } else {
                                            MyToast.showToast("指定整改人失败，请检查网络信息");
                                        }
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<SuccessBean> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );

                }
            }
        });
    }

    @Override
    public void show() {
        super.show();
        Window dialogwindow = this.getWindow();
        WindowManager.LayoutParams lp = dialogwindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogwindow.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);
    }


}
